package com.showgood.util {
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.getQualifiedClassName;
	import mx.graphics.shaderClasses.ColorBurnShader_ShaderClass;
	
	/**
	 * load resource by http
	 * load class, bitmap from swf
	 * TODO: other type resources
	 * 
	 * @author BianPeng
	 */
	public class NetResLoader extends EventDispatcher {
		private var _url:String;
		private var _symbols:Array;
		private var _name:String;
		
		import org.as3commons.logging.getLogger;
		import org.as3commons.logging.ILogger;
		private var _logger:ILogger = getLogger(this);
		
		public function NetResLoader(name:String, url:String, symbols:Array = null)  {
			_name = name;
			_url = url;
			_symbols = symbols;
		}
		
		public function load():void {
			var _loader:Loader = new Loader();
			var _contenxt:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			setupEventHandlers(_loader);
			_logger.info('prepare load: ' + _url);
			_loader.load(new URLRequest(_url), _contenxt);
		}
		
		private function setupEventHandlers(loader:Loader):void {
			var info:LoaderInfo = loader.contentLoaderInfo;
			info.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
			info.addEventListener(Event.OPEN, this.onOpen);
			info.addEventListener(Event.COMPLETE, this.onComplete);
			info.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
			info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError);
		}
		
		private function removeEventHandlers(loader:Loader):void {
			var info:LoaderInfo = loader.contentLoaderInfo;
			info.removeEventListener(Event.COMPLETE,onComplete);
			info.removeEventListener(ProgressEvent.PROGRESS,onProgress);
			info.removeEventListener(IOErrorEvent.IO_ERROR,onError);
			info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onError);
		}
		
		private function onOpen(event:Event):void {
			_logger.info('start load: ' + _url);
		}
		
		private function onProgress(event:ProgressEvent):void {
			_logger.info('loading: ' + Math.round((event.bytesLoaded / event.bytesTotal)*100) + '%');
		}
		
		private function onComplete(event:Event):void {
			_logger.info('load complete: ' + _url);
			var _loader:LoaderInfo = event.target as LoaderInfo;
			handlerComplete(_loader);
		}
		
		private function onError(event:Event):void {
			_logger.error('resource load error: ' + _url);
		}
		
		//
		// 请求结束, 并发送事件
		//
		private function handlerComplete(info:LoaderInfo):void {
			// 把结果中的类信息, 加载出来
			if (_symbols != null) {
				var _clazz:Array = new Array();
				var _domain:ApplicationDomain = info.applicationDomain;
				for (var i:int = 0 ; i < _symbols.length; ++i) {
					var _clz:Object = _domain.getDefinition(_symbols[i]);
					_logger.info('class loaded: ' + getQualifiedClassName(_clz));
					if (_clz) {
						_clazz.push(_clz);
					}
				}
			}
			// 完成, 通知一下
			var _event:NetResLoaderEvent = new NetResLoaderEvent(NetResLoaderEvent.COMPLETE);
			_event._data = _clazz;
			_event._url = this._url;
			_event._content = info.content;
			_event._name = _name;
			this.dispatchEvent(_event);
		}
	}
}

